package com.link.base.base.member.service;

import com.link.base.base.member.dao.mybatis.mapper.GiftCardPageMapper;
import com.link.base.base.member.model.GiftCardPage;
import com.link.base.base.member.model.GiftCardPageTheme;
import com.link.base.base.member.model.PageThemeItem;
import com.link.base.wechat.wxcard.model.*;
import com.link.base.wechat.wxcard.service.WxCardInfoService;
import com.link.base.wechat.wxplatform.model.WxPlatform;
import com.link.base.wechat.wxplatform.service.WxPlatformService;
import com.link.base.wechat.wxtoken.service.WxTokenService;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.JsonUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 礼品卡货架对象
 *
 * @author 路江
 * @date 2019/03/02 10:25
 */
@Service
public class GiftCardPageServiceImpl extends BasicServiceImpl<GiftCardPage> implements GiftCardPageService {

    @Resource
    private GiftCardPageMapper giftCardPageMapper;

    @Resource
    private GiftCardPageThemeService giftCardPageThemeService;
    @Resource
    private PageThemeItemService pageThemeItemService;

    @Resource
    private WxPlatformService wxPlatformService;

    @Resource
    private WxTokenService wxTokenService;

    @Resource
    private WxCardInfoService wxCardInfoService;

    
    @Override
    public BasicMapper<GiftCardPage> getBasicMapper() {
        return giftCardPageMapper;
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public Map<String, Object> createGiftCardPage(Page page, String rowStatus) throws Exception {
        // 创建礼品卡货架
        Long wxId = page.getWxPlatformId();
        WxPlatform wxPlatform = new WxPlatform();
        wxPlatform.setId(page.getWxPlatformId());
        wxPlatform = wxPlatformService.queryById(wxPlatform);
        // 公众号信息置空
        page.setWxPlatformId(null);
        page.setWxPlatformName(null);
        page.setLinkRequestId(null);
        String pageString = JsonUtil.toString(page);
        setPageToNull(page);
        Map<String, Object> map = wxCardInfoService.upsertGiftCardPage(wxPlatform.getWx_app_id(),page,rowStatus);
        if (StringUtils.equals(map.get("result").toString(),"1")) {
            page = JsonUtil.fromJsonStringToT(pageString,Page.class);
            GiftCardPage cardPage = new GiftCardPage();
            // 如果是修改时，定义原货架对象
            GiftCardPage giftCardPage = new GiftCardPage();
            if (StringUtils.isBlank(page.getPageId())) {
                cardPage.setPageId(map.get("page_id").toString());
            } else {
                cardPage.setPageId(page.getPageId());
                giftCardPage.setPageId(page.getPageId());
                giftCardPage = this.queryPageByPageId(giftCardPage);
            }
            cardPage.setStatus("Active");
            cardPage.setWxId(wxId);
            cardPage.setAddress(page.getAddress());
            cardPage.setBannerPicUrl(page.getBannerPicUrl());
            cardPage.setPreBannerPicUrl(page.getPreBannerPicUrl());
            cardPage.setPageTitle(page.getPageTitle());
            cardPage.setBizDescription(page.getBizDescription());
            cardPage.setServicePhone(page.getServicePhone());
            if (null != page.getSupportBuyForSelf() && page.getSupportBuyForSelf()) {
                cardPage.setSupportBuyForSelf("Y");
            } else if (null != page.getSupportBuyForSelf() && !page.getSupportBuyForSelf()) {
                cardPage.setSupportBuyForSelf("N");
            }
            if (null != page.getSupportMulti() && page.getSupportMulti()) {
                cardPage.setSupportMulti("Y");
            } else if (null != page.getSupportMulti() && !page.getSupportMulti()) {
                cardPage.setSupportMulti("N");
            }
            // cellData
            if (null != page.getCell_1()) {
                cardPage.setCellTitlea(page.getCell_1().getTitle());
                cardPage.setCellUrla(page.getCell_1().getUrl());
            }
            if (null != page.getCell_2()) {
                cardPage.setCellTitleb(page.getCell_2().getTitle());
                cardPage.setCellUrlb(page.getCell_2().getUrl());
            }
            this.insert(cardPage);
            // 保存主题结构体
            for (int i = 0;i < page.getThemeList().size();i++) {
                ThemeData themeData = page.getThemeList().get(i);
                GiftCardPageTheme theme = new GiftCardPageTheme();
                theme.setCategoryIndex(themeData.getCategoryIndex());
                for (int n = 0 ; n < page.getCategoryList().size();n++) {
                    if (n == i) {
                        theme.setThemeIndexTitle(page.getCategoryList().get(n).getTitle());
                    }
                }
                if (null != themeData.getIsBanner() && themeData.getIsBanner()) {
                    theme.setIsbanner("Y");
                } else if (null != themeData.getIsBanner() && !themeData.getIsBanner()) {
                    theme.setIsbanner("N");
                }
                theme.setThemePicUrl(themeData.getThemePicUrl());
                theme.setPreThemePicUrl(themeData.getPreThemePicUrl());
                theme.setThemeTitle(themeData.getTitle());
                theme.setTitleColor(themeData.getTitleColor());
                theme.setItemId(String.valueOf(cardPage.getId()));
                giftCardPageThemeService.insert(theme);
                // 保存pic_item_list卡面结构体
                for (int j = 0;j < themeData.getPicItemList().size();j++) {
                    PageThemeItem pageThemeItem = new PageThemeItem();
                    pageThemeItem.setBackgroundPicUrl(themeData.getPicItemList().get(j).getBackgroundPicUrl());
                    pageThemeItem.setPreBackgroundPicUrl(themeData.getPicItemList().get(j).getPreBackgroundPicUrl());
                    pageThemeItem.setDefaultGiftingMsg(themeData.getPicItemList().get(j).getDefaultGiftingMsg());
                    pageThemeItem.setItemId(String.valueOf(theme.getId()));
                    pageThemeItem.setItemType("pic_item_list");
                    pageThemeItemService.insert(pageThemeItem);
                }
                // 保存item_list商品结构体
                for (int j = 0;j < themeData.getItemList().size();j++) {
                    PageThemeItem pageThemeItem = new PageThemeItem();
                    pageThemeItem.setCardId(themeData.getItemList().get(j).getCardId());
                    pageThemeItem.setGoodsDesc(themeData.getItemList().get(j).getDesc());
                    pageThemeItem.setGoodsPicUrl(themeData.getItemList().get(j).getPicUrl());
                    pageThemeItem.setPreGoodsPicUrl(themeData.getItemList().get(j).getPrePicUrl());
                    pageThemeItem.setGoodsTitle(themeData.getItemList().get(j).getTitle());
                    pageThemeItem.setItemId(String.valueOf(theme.getId()));
                    pageThemeItem.setItemType("item_list");
                    pageThemeItemService.insert(pageThemeItem);
                }
            }
            // 删除原货架对象
            if (StringUtils.equals(rowStatus,"UPDATE")) {
                this.deleteCardPage(giftCardPage);
            }
        }
        return map;
    }

    private void setPageToNull(Page page) {

        page.setPreBannerPicUrl(null);
        for (ThemeData themeData : page.getThemeList()) {
            themeData.setPreThemePicUrl(null);
            for (PicItemData picItemData : themeData.getPicItemList()) {
                picItemData.setPreBackgroundPicUrl(null);
            }
            for (ItemData itemData : themeData.getItemList()) {
                itemData.setPrePicUrl(null);
            }
        }
    }

    @Override
    public Map<String, Object> queryGiftCardPageById(GiftCardPage giftCardPage) throws Exception {
        Map<String, Object> map = new HashMap<>(16);
        giftCardPage = this.queryById(giftCardPage);
        GiftCardPageTheme theme = new GiftCardPageTheme();
        theme.setItemId(String.valueOf(giftCardPage.getId()));
        List<GiftCardPageTheme> themeList = giftCardPageThemeService.queryByExamplePage(theme);

        Page page = new Page();
        // 货架基本信息
        page.setPageId(giftCardPage.getPageId());
        page.setAddress(giftCardPage.getAddress());
        page.setBannerPicUrl(giftCardPage.getBannerPicUrl());
        page.setPreBannerPicUrl(giftCardPage.getPreBannerPicUrl());
        page.setBizDescription(giftCardPage.getBizDescription());
        page.setPageTitle(giftCardPage.getPageTitle());
        page.setServicePhone(giftCardPage.getServicePhone());
        if (StringUtils.equals(giftCardPage.getSupportBuyForSelf(),"Y")) {
            page.setSupportBuyForSelf(true);
        } else if (StringUtils.equals(giftCardPage.getSupportBuyForSelf(),"N")) {
            page.setSupportBuyForSelf(false);
        }
        if (StringUtils.equals(giftCardPage.getSupportMulti(),"Y")) {
            page.setSupportMulti(true);
        } else if (StringUtils.equals(giftCardPage.getSupportMulti(),"N")) {
            page.setSupportMulti(false);
        }

        // 货架主题信息
        List<ThemeData> themeDataList = new ArrayList<>();
        List<String> indexTitle = new ArrayList<>();
        List<CategoryData> categoryDataList = new ArrayList<>();
        for (GiftCardPageTheme giftCardPageTheme : themeList) {
            //
            ThemeData themeData = new ThemeData();
            themeData.setCategoryIndex(giftCardPageTheme.getCategoryIndex());
            if (StringUtils.equals(giftCardPageTheme.getIsbanner(),"Y")) {
                themeData.setIsBanner(true);
            } else if (StringUtils.equals(giftCardPageTheme.getIsbanner(),"N")) {
                themeData.setIsBanner(false);
            }
            themeData.setThemePicUrl(giftCardPageTheme.getThemePicUrl());
            themeData.setPreThemePicUrl(giftCardPageTheme.getPreThemePicUrl());
            themeData.setTitle(giftCardPageTheme.getThemeTitle());
            themeData.setTitleColor(giftCardPageTheme.getTitleColor());
            // pic_item_list卡面结构体；item_list商品结构体
            PageThemeItem pageThemeItem = new PageThemeItem();
            pageThemeItem.setItemId(String.valueOf(giftCardPageTheme.getId()));
            List<PageThemeItem> list = pageThemeItemService.queryByExamplePage(pageThemeItem);
            //
            List<ItemData> item_list = new ArrayList<>();
            List<PicItemData> pic_item_list = new ArrayList<>();
            for (PageThemeItem item : list) {
                if (StringUtils.equals(item.getItemType(),"pic_item_list")) {
                    PicItemData picItemData = new PicItemData();
                    picItemData.setBackgroundPicUrl(item.getBackgroundPicUrl());
                    picItemData.setPreBackgroundPicUrl(item.getPreBackgroundPicUrl());
                    picItemData.setDefaultGiftingMsg(item.getDefaultGiftingMsg());
                    pic_item_list.add(picItemData);
                } else if (StringUtils.equals(item.getItemType(),"item_list")) {
                    ItemData itemData = new ItemData();
                    itemData.setCardId(item.getCardId());
                    itemData.setDesc(item.getGoodsDesc());
                    itemData.setPicUrl(item.getGoodsPicUrl());
                    itemData.setPrePicUrl(item.getPreGoodsPicUrl());
                    itemData.setTitle(item.getGoodsTitle());
                    item_list.add(itemData);
                }
            }
            themeData.setItemList(item_list);
            themeData.setPicItemList(pic_item_list);
            themeDataList.add(themeData);

            // 主题分类集合
            CategoryData categoryData = new CategoryData();
            categoryData.setTitle(giftCardPageTheme.getThemeIndexTitle());
            if (!indexTitle.contains(giftCardPageTheme.getThemeIndexTitle())
                    && StringUtils.isNotBlank(giftCardPageTheme.getThemeIndexTitle())) {
                categoryDataList.add(categoryData);
            }
            indexTitle.add(giftCardPageTheme.getThemeIndexTitle());

        }
        page.setCategoryList(categoryDataList);
        page.setThemeList(themeDataList);
        page.setWxPlatformName(giftCardPage.getWxName());
        page.setWxPlatformId(giftCardPage.getWxId());
        CellData cellData1 = new CellData();
        cellData1.setTitle(giftCardPage.getCellTitlea());
        cellData1.setUrl(giftCardPage.getCellUrla());
        CellData cellData2 = new CellData();
        cellData2.setTitle(giftCardPage.getCellTitleb());
        cellData2.setUrl(giftCardPage.getCellUrlb());
        if (StringUtils.isNotBlank(giftCardPage.getCellTitlea())) {
            page.setCell_1(cellData1);
        }
        if (StringUtils.isNotBlank(giftCardPage.getCellTitleb())) {
            page.setCell_2(cellData2);
        }

        map.put("rows",page);
        return map;
    }

    @Override
    public void deleteCardPage(GiftCardPage giftCardPage) throws Exception {
        // 删除货架对象并且删除货架下所有子对象
        GiftCardPageTheme pageTheme = new GiftCardPageTheme();
        pageTheme.setItemId(String.valueOf(giftCardPage.getId()));
        List<GiftCardPageTheme> themeList = giftCardPageThemeService.queryByExamplePage(pageTheme);
        for (GiftCardPageTheme cardPageTheme : themeList) {
            PageThemeItem item = new PageThemeItem();
            item.setItemId(String.valueOf(cardPageTheme.getId()));
            List<PageThemeItem> itemList = pageThemeItemService.queryByExamplePage(item);
            for (PageThemeItem pageThemeItem : itemList) {
                pageThemeItemService.deleteById(pageThemeItem);
            }
            giftCardPageThemeService.deleteById(cardPageTheme);

        }
        deleteById(giftCardPage);

    }

    @Override
    public GiftCardPage queryPageByPageId(GiftCardPage page) throws Exception {
        return giftCardPageMapper.queryPageByPageId(page);
    }
}

